package com.javalei.business;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;
import java.util.UUID;


public class RegistMail implements  Runnable{

    //收件人InternetAddress 数组
    private InternetAddress [] userInternetAddress;
    //构成函数提供properties对象
    private Properties properties;
    //邮箱发送session对象
   private  Session session;
    //Transport 对象
   private Transport  transport;
    //发件人InternetAddress对象
    private InternetAddress myInternetAddress;
    //邮件服务器地址
    private String host ="smtp.qq.com";
    //邮件服务器端口
    private int port=465;
    //用户名
    private String  username="454467197@qq.com";
    //授权码
    private String code="qetgrsfsdguybjih";



    public  RegistMail(InternetAddress [] userInternetAddress){
        this.userInternetAddress=userInternetAddress;
        properties=new Properties();
        properties.put("mail.debug",true);
        properties.put("mail.smtp.auth","true");
        properties.put("mail.transport.protocol","smtp");
        properties.put("mail.smtp.host", "smtp.qq.com");
        properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtp.port", "465");
        properties.put("mail.smtp.socketFactory.port", "465");
        session=Session.getInstance(properties);
        session.setDebug(true);

        try {
            myInternetAddress=new InternetAddress("454467197@qq.com");
        } catch (AddressException e) {
            e.printStackTrace();
            // TODO wirte log
        }
    }

    public static void main(String[] args) throws AddressException {
        System.out.println(UUID.randomUUID().toString().replace("-","")+UUID.randomUUID().toString().replace("-",""));
        // Thread a=new Thread(new RegistMail(new InternetAddress[]{new InternetAddress("454467197@qq.com")}));
        //a.start();
    }

    public String getHtml(String username){
        StringBuilder demo = new StringBuilder();
        demo.append("<div style=\"background-color:#ECECEC; padding: 35px;\">")
                .append("<table cellpadding=\"0\" align=\"center\" style=\"width: 600px; margin: 0px auto; text-align: left; position: relative; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; font-size: 14px; font-family:微软雅黑, 黑体; line-height: 1.5; box-shadow: rgb(153, 153, 153) 0px 0px 5px; border-collapse: collapse; background-position: initial initial; background-repeat: initial initial;background:#fff;\">\n")
                .append("<tbody>")
                .append("<tbody>")
                .append("<tr>")
                .append("<th valign=\"middle\" style=\"height: 25px; line-height: 25px; padding: 15px 35px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #C46200; background-color: #FEA138; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px;\">\n")
                .append("<font face=\"微软雅黑\" size=\"5\" style=\"color: rgb(255, 255, 255); \">MaoYoo! （react.com）</font>")
                .append("</th>")
                .append("</tr>")
                .append("<tr>")
                .append("<td>")
                .append("<div style=\"padding:25px 35px 40px; background-color:#fff;\">")
                .append("<h2 style=\"margin: 5px 0px; \"><font color=\"#333333\" style=\"line-height: 20px; \"><font style=\"line-height: 22px; \" size=\"4\">亲爱的 "+username+"：</font></font></h2>")
                .append("我谨代表吴彦祖先生真诚的向你道歉")
                .append("</div>")
                .append("</td>")
                .append("</tr>")
                .append("</tbody>")
                .append("</table>")
                .append("</div>");


        return  demo.toString();
    }
   @Override
    public void run() {

        //创建message对象
        Message  message= new MimeMessage(session);

        try {
            Multipart  multipart=new MimeMultipart();
            message.setContent(getHtml("小可爱"),"text/html; charset=utf-8");
            //设置发件人
        message.setFrom(myInternetAddress);

        message.addRecipient(Message.RecipientType.TO,
                myInternetAddress);
        transport=session.getTransport();

        transport.connect(host,port,username,code);

        transport.sendMessage(message,userInternetAddress);
        } catch (MessagingException e) {
            e.printStackTrace();
        }finally {
            try {
                transport.close();
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
    }
}

